package org.javaboy.vhr.controller.personnel;

import org.javaboy.vhr.model.AttendanceRecord;
import org.javaboy.vhr.model.RespBean;
import org.javaboy.vhr.service.AttendanceRecordService;
import org.javaboy.vhr.utils.HrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/attendance")
public class AttendanceRecordController {

    @Autowired
    private AttendanceRecordService attendanceRecordService;

    @PostMapping("/clock")
    public RespBean clockIn(@RequestParam String clockType, @RequestParam String status,@RequestParam String remark) {
        try {
            // 获取当前登录用户的ID
            Integer currentEid = HrUtils.getCurrentHr().getId();

            // 调用服务层方法插入打卡记录
            int result = attendanceRecordService.insertAttendanceRecord(currentEid, clockType, status,remark);

            if (result > 0) {
                return RespBean.ok("打卡成功");
            } else {
                return RespBean.error("打卡失败");
            }
        } catch (IllegalArgumentException e) {
            return RespBean.error(e.getMessage());
        } catch (Exception e) {
            return RespBean.error("打卡异常：" + e.getMessage());
        }
    }

    @GetMapping("/records")
    public List<AttendanceRecord> getAllRecords() {
        return attendanceRecordService.getAllAttendanceRecords();
    }

    @GetMapping("/records/id/{eid}")
    public List<AttendanceRecord> getRecordsByEid(@PathVariable Integer eid) {
        return attendanceRecordService.getAttendanceRecordsByEid(eid);
    }
    @GetMapping("/records/name/{employeeName}")
    public List<AttendanceRecord> getRecordsByName(@PathVariable String employeeName) {
        return attendanceRecordService.getAttendanceRecordsByEname(employeeName);
    }

    @DeleteMapping("/record/delete/{id}")
    public RespBean deleteRecord(@PathVariable Integer id) {
        System.out.println("删除的ID：" + id);

        int result = attendanceRecordService.deleteAttendanceRecord(id);
        if (result > 0) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    @PutMapping("/record/update")
    public RespBean updateRecord(@RequestBody AttendanceRecord attendanceRecord) {
        int result = attendanceRecordService.updateAttendanceRecord(attendanceRecord);
        if (result > 0) {
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }
}
